#include "sys.h"
void touch_init(void)
{
    GPIO_InitTypeDef __xdata GPIO_InitStructure;                //结构定义
    GPIO_InitStructure.Pin = TOUCH_PIN_POWER | TOUCH_PIN_MODE | TOUCH_PIN_SPEED | TOUCH_PIN_UP | TOUCH_PIN_DOWN | TOUCH_PIN_PAGE;                 //指定要初始化的IO,
    GPIO_InitStructure.Mode = GPIO_HighZ;                       //指定IO的输入或输出方式
    GPIO_Inilize(TOUCH_PORT_PAGE, &GPIO_InitStructure);      //初始化
}

void touch_func(void)
{
    static u8 __xdata oldState;
    static u8 __xdata isNeedReleased;       //识别到一个有效按键后需要释放才能进行下次按键输入
    static u8 __xdata btnPowerPressedCnt;
    static u8 __xdata btnModePressedCnt;
    static u8 __xdata btnWindSpeedPressedCnt;
    static u8 __xdata btnDownPressedCnt;
    static u8 __xdata btnUpPressedCnt;
    static u8 __xdata btnPagePressedCnt;
    static u8 __xdata btnNoOperationCnt;
    
    if(IS_TOUCHED())  //有按键按下
    {
        btnNoOperationCnt = 0;
        sysParams.isSaving = 0;
        /* 唤醒操作，此次按键不作处理 */
        if(sysParams.isNoOperation)
        {
            sysParams.isNoOperation = 0;
            return;
        }
    }
    else    //无按键操作
    {
        if(btnNoOperationCnt < 150)
        {
            btnNoOperationCnt++;
        }
        else
        {
            sysParams.isNoOperation = 1;
            sysParams.isSaving = 1;
        }
    }

    if((isNeedReleased & BIT_STATE_POWER) == 0)
    {
        if((oldState & BIT_STATE_POWER) == 0)   //原先是松开状态
        {
            if(IS_TOUCHED_POWER())  //检测到按下
            {
                oldState |= BIT_STATE_POWER;  
            }
        }
        else    //原先是按下状态
        {
            if(IS_TOUCHED_POWER())  //仍检测到按下
            {
                btnPowerPressedCnt++;

                if(btnPowerPressedCnt >= LONG_PRESSED_3S_CNT) //超过长按3s的判断阈值
                {
                    sysParams.btn.btnPower = LONG_PRESSED_3S; //按键长按
                    sysParams.buzzer = 1;
                    btnPowerPressedCnt = 0;
                    oldState &= ~BIT_STATE_POWER;
                    isNeedReleased |= BIT_STATE_POWER;
                    PrintString1("power long\n\r");
                }
            }   //按键松开
            else
            {
                if(btnPowerPressedCnt < LONG_PRESSED_3S_CNT)
                {
                    sysParams.btn.btnPower = PRESSED;//按键按下
                    sysParams.buzzer = 1;
                    btnPowerPressedCnt = 0;
                    oldState &= ~BIT_STATE_POWER;
                    PrintString1("power\n\r");
                }
            }
        }
    }
    else
    {
        if(IS_TOUCHED_POWER() == 0)  //检测到按键释放
        {
            isNeedReleased &= ~BIT_STATE_POWER;  
        }
    }
    
    
    if(sysParams.regs.isBtnLocked == 0) //按键未上锁
    {
        if((isNeedReleased & BIT_STATE_MODE) == 0)
        {
            if((oldState & BIT_STATE_MODE) == 0)   //原先是松开状态
            {
                if(IS_TOUCHED_MODE())  //检测到按下
                {
                    oldState |= BIT_STATE_MODE;
                }
            }
            else    //原先是按下状态
            {
                if(IS_TOUCHED_MODE())  //仍检测到按下
                {
                    btnModePressedCnt++;

                    if(btnModePressedCnt >= LONG_PRESSED_6S_CNT) //超过长按的判断阈值
                    {
                        sysParams.btn.btnMode = LONG_PRESSED_6S;//按键长按
                        sysParams.buzzer = 1;
                        btnModePressedCnt = 0;
                        oldState &= ~BIT_STATE_MODE;
                        isNeedReleased |= BIT_STATE_MODE;
                        PrintString1("mode long\n\r");
                    }
                }   //按键松开
                else
                {
                    if(btnModePressedCnt < LONG_PRESSED_6S_CNT)
                    {
                        sysParams.btn.btnMode = PRESSED;//按键按下
                        sysParams.buzzer = 1;
                        btnModePressedCnt = 0;
                        oldState &= ~BIT_STATE_MODE;
                        PrintString1("mode\n\r");
                    }
                }
            }
        }
        else
        {
            if(IS_TOUCHED_MODE() == 0)  //检测到按键释放
            {
                isNeedReleased &= ~BIT_STATE_MODE;  
            }
        }

        if((isNeedReleased & BIT_STATE_SPEED) == 0)
        {
            if((oldState & BIT_STATE_SPEED) == 0)   //原先是松开状态
            {
                if(IS_TOUCHED_SPEED())  //检测到按下
                {
                    oldState |= BIT_STATE_SPEED;  
                }
            }
            else    //原先是按下状态
            {
                if(IS_TOUCHED_SPEED())  //仍检测到按下
                {
                    btnWindSpeedPressedCnt++;

                    if(btnWindSpeedPressedCnt >= LONG_PRESSED_6S_CNT) //超过长按的判断阈值
                    {
                        sysParams.btn.btnWindSpeed = LONG_PRESSED_6S;//按键长按
                        sysParams.buzzer = 1;
                        btnWindSpeedPressedCnt = 0;
                        oldState &= ~BIT_STATE_SPEED;
                        isNeedReleased |= BIT_STATE_SPEED;
                        PrintString1("wind long\n\r");
                    }
                }   //按键松开
                else
                {
                    if(btnWindSpeedPressedCnt < LONG_PRESSED_6S_CNT)
                    {
                        sysParams.btn.btnWindSpeed = PRESSED;//按键按下
                        sysParams.buzzer = 1;
                        btnWindSpeedPressedCnt = 0;
                        oldState &= ~BIT_STATE_SPEED;
                        PrintString1("wind\n\r");
                    }
                }
            }
        }
        else
        {
            if(IS_TOUCHED_SPEED() == 0)  //检测到按键释放
            {
                isNeedReleased &= ~BIT_STATE_SPEED;  
            }
        }
        
        if((isNeedReleased & BIT_STATE_UP) == 0)
        {
            if((oldState & BIT_STATE_UP) == 0)   //原先是松开状态
            {
                if(IS_TOUCHED_UP())  //检测到按下
                {
                    oldState |= BIT_STATE_UP;  
                }
            }
            else    //原先是按下状态
            {
                if(IS_TOUCHED_UP())  //仍检测到按下
                {
                    btnUpPressedCnt++;

                    if(btnUpPressedCnt >= LONG_PRESSED_6S_CNT) //超过长按的判断阈值
                    {
                        sysParams.btn.btnUp = LONG_PRESSED_6S;//按键长按
                        sysParams.buzzer = 1;
                        btnUpPressedCnt = 0;
                        oldState &= ~BIT_STATE_UP;
                        isNeedReleased |= BIT_STATE_UP;
                        PrintString1("up long\n\r");
                    }
                }   //按键松开
                else
                {
                    if(btnUpPressedCnt < LONG_PRESSED_6S_CNT)
                    {
                        sysParams.btn.btnUp = PRESSED;//按键按下
                        sysParams.buzzer = 1;
                        btnUpPressedCnt = 0;
                        oldState &= ~BIT_STATE_UP;
                        PrintString1("up\n\r");
                    }
                }
            }
        }
        else
        {
            if(IS_TOUCHED_UP() == 0)  //检测到按键释放
            {
                isNeedReleased &= ~BIT_STATE_UP;  
            }
        }

        if((isNeedReleased & BIT_STATE_DOWN) == 0)
        {
            if((oldState & BIT_STATE_DOWN) == 0)   //原先是松开状态
            {
                if(IS_TOUCHED_DOWN())  //检测到按下
                {
                    oldState |= BIT_STATE_DOWN;  
                }
            }
            else    //原先是按下状态
            {
                if(IS_TOUCHED_DOWN())  //仍检测到按下
                {
                    btnDownPressedCnt++;

                    if(btnDownPressedCnt >= LONG_PRESSED_6S_CNT) //超过长按的判断阈值
                    {
                        sysParams.btn.btnDown = LONG_PRESSED_6S;//按键长按
                        sysParams.buzzer = 1;
                        btnDownPressedCnt = 0;
                        oldState &= ~BIT_STATE_DOWN;
                        isNeedReleased |= BIT_STATE_DOWN;
                        PrintString1("down long\n\r");
                    }
                }   //按键松开
                else
                {
                    if(btnDownPressedCnt < LONG_PRESSED_6S_CNT)
                    {
                        sysParams.btn.btnDown = PRESSED;//按键按下
                        sysParams.buzzer = 1;
                        btnDownPressedCnt = 0;
                        oldState &= ~BIT_STATE_DOWN;
                        PrintString1("down\n\r");
                    }
                }
            }
        }
        else
        {
            if(IS_TOUCHED_DOWN() == 0)  //检测到按键释放
            {
                isNeedReleased &= ~BIT_STATE_DOWN;  
            }
        }

        if((isNeedReleased & BIT_STATE_PAGE) == 0)
        {
            if((oldState & BIT_STATE_PAGE) == 0)   //原先是松开状态
            {
                if(IS_TOUCHED_PAGE())  //检测到按下
                {
                    oldState |= BIT_STATE_PAGE;  
                }
            }
            else    //原先是按下状态
            {
                if(IS_TOUCHED_PAGE())  //仍检测到按下
                {
                    btnPagePressedCnt++;

                    if(btnPagePressedCnt >= LONG_PRESSED_6S_CNT) //超过长按的判断阈值
                    {
                        sysParams.btn.btnPage = LONG_PRESSED_6S;//按键长按
                        sysParams.buzzer = 1;
                        btnPagePressedCnt = 0;
                        oldState &= ~BIT_STATE_PAGE;
                        isNeedReleased |= BIT_STATE_PAGE;
                        PrintString1("page long\n\r");
                    }
                }   //按键松开
                else
                {
                    if(btnPagePressedCnt < LONG_PRESSED_6S_CNT)
                    {
                        sysParams.btn.btnPage = PRESSED;//按键按下
                        sysParams.buzzer = 1;
                        btnPagePressedCnt = 0;
                        oldState &= ~BIT_STATE_PAGE;
                        PrintString1("page\n\r");
                    }
                }
            }
        }
        else
        {
            if(IS_TOUCHED_PAGE() == 0)  //检测到按键释放
            {
                isNeedReleased &= ~BIT_STATE_PAGE;  
            }
        }
    }
}